/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TreatmentPanel.java
 *
 * Created on 08.07.2009, 0:46:00
 */

package ru.vsu.cs.piit.vokod.view.panels;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JComboBox;
import ru.vsu.cs.piit.vokod.TableModels.CBModel;
import ru.vsu.cs.piit.vokod.controller.Controller;
import ru.vsu.cs.piit.vokod.controller.Maps;
import ru.vsu.cs.piit.vokod.model.GroupOfTreatment;
import ru.vsu.cs.piit.vokod.model.Treatment;

/**
 *
 * @author Галчонок
 */
public class TreatmentPanel extends javax.swing.JPanel {
    Treatment treatment;

    /** Creates new form TreatmentPanel */
    public TreatmentPanel() {
        this(new Treatment());
    }

    public TreatmentPanel(Treatment treatment){
        this.treatment = treatment;
        initComponents();
        SetValues();
        AddActions();
        setSize(getPreferredSize());
    }

    void AddActions(){
        tfCode.addFocusListener(new FocusListener() {

            public void focusGained(FocusEvent e) {}

            public void focusLost(FocusEvent e) {
                treatment.setCode(tfCode.getText());
            }
        });

        tfName.addFocusListener(new FocusListener() {

            public void focusGained(FocusEvent e) {}

            public void focusLost(FocusEvent e) {
                treatment.setName(tfName.getText());
            }
        });

        cbGroup.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                treatment.setGroupOfTreatment((GroupOfTreatment)cbGroup.getModel().getSelectedItem());
            }
        });

        jAddGroupOfTreatmentBtn.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                Controller.getInstance().addGroupOfTreatment();
            }
        });
    }

    void SetValues(){
        tfName.setText(treatment.getName());
        tfCode.setText(treatment.getCode());
        cbGroup.getModel().setSelectedItem(treatment.getGroupOfTreatment());
    }

    public Treatment getTreatment(){
        return treatment;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        tfName = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        tfCode = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        cbGroup = new JComboBox(new CBModel(Maps.getGroupOfTreatmentsList()));
        jAddGroupOfTreatmentBtn = new javax.swing.JButton();

        jLabel1.setText("Название вида лечения");

        jLabel2.setText("Код");

        jLabel3.setText("Относится к группе:");

        jAddGroupOfTreatmentBtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ru/vsu/cs/piit/vokod/resources/add.gif"))); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tfName, javax.swing.GroupLayout.DEFAULT_SIZE, 254, Short.MAX_VALUE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(tfCode, javax.swing.GroupLayout.DEFAULT_SIZE, 254, Short.MAX_VALUE)
                    .addComponent(jLabel3)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(cbGroup, 0, 230, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jAddGroupOfTreatmentBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tfName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tfCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jAddGroupOfTreatmentBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbGroup, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cbGroup;
    private javax.swing.JButton jAddGroupOfTreatmentBtn;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField tfCode;
    private javax.swing.JTextField tfName;
    // End of variables declaration//GEN-END:variables

}
